package com.itjiange.springbootinit.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.itjiange.springbootinit.common.ErrorCode;
import com.itjiange.springbootinit.exception.ThrowUtils;
import com.itjiange.springbootinit.model.dto.ai.ChatGptRequest;
import com.itjiange.springbootinit.model.dto.ai.ChatRequest;
import com.itjiange.springbootinit.model.dto.ai.ChatResponse;

/**
 * 利用 ai 生成图表工具
 *
 * @author ItJiange
 * @date 2023/12/7
 */
public class ChartAIUtils {

    public static String chatGpt(String message) {
        String apiHost = "https://ycdl.ysywy.shop/v1/chat/completions";
        ChatGptRequest chatGptRequest = new ChatGptRequest(message);
        String json = JSONUtil.toJsonStr(chatGptRequest);
//        String json = "{\n" +
//                "   \"model\": \"gpt-3.5-turbo\",\n" +
//                "   \"messages\": [{\"role\": \"user\", \"content\": \"" + content + message + "\"}],\n" +
//                "   \"temperature\": 0.7\n" +
//                "   }";
        String apiKey = "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";
        System.out.println(json);
        String result = HttpRequest.post(apiHost)
                .header(Header.AUTHORIZATION, "Bearer " + apiKey)
                .header(Header.CONTENT_TYPE, "application/json")
                .body(json)
                .execute().body();
        System.out.println("\n响应结果：" + result + "\n");
        return new JSONObject(result).getJSONArray("choices").getJSONObject(0).getJSONObject("message").getStr("content");
    }

    public static void main(String[] args) {
        System.out.println(chat("Hello"));
    }

    /**
     * AI 生成图表
     *
     * @param q
     * @return
     */
    public static ChatResponse chat(String q) {
        String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";//这里看官方文档
        String apiKey = "sk-c6a47d0bc92d40b1b6a3c49fa9ce09a9";

        ChatRequest chatRequest = new ChatRequest(q);
        String json = JSONUtil.toJsonStr(chatRequest);
        System.out.println("JSON 数据：" + json); //正式发送给api前,查看请求的主要数据情况
        String result = HttpRequest.post(url)
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .body(json)
                .execute().body();
        ChatResponse chatResponse = JSONUtil.toBean(result, ChatResponse.class);
        ThrowUtils.throwIf(ObjectUtil.isEmpty(chatResponse.getOutput()), ErrorCode.SYSTEM_ERROR, "AI 无响应");
        String resText = chatResponse.getOutput().text;
//        resText = resText
//                .replaceAll("javascript", "")
//                .replaceAll("```", "")
//                .replaceAll("json", "")
//                .replaceAll("css", "");
        System.out.println(resText);
        ChatResponse.Output output = chatResponse.getOutput();
        output.setText(resText);
        chatResponse.setOutput(output);
        return chatResponse;
    }
}
